package com.springboot.service;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.*;
import com.springboot.dto.VerifyDTO;
import com.springboot.dto.VerifyDemandDTO;
import com.springboot.repository.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PlatformService {

    private final UserRepository userRepository;
    private final PersonRepository personRepository;
    private final EnterpriseRepository enterpriseRepository;
    private final BankRepository bankRepository;
    private final DemandSubRepository demandSubRepository;
    private final DemandRepository demandRepository;
    private final FileRepository fileRepository;
    private final TaskRepository taskRepository;
    @Autowired
    public PlatformService(PersonRepository personRepository,EnterpriseRepository enterpriseRepository,BankRepository bankRepository,UserRepository userRepository,DemandSubRepository demandSubRepository,DemandRepository demandRepository,FileRepository fileRepository,TaskRepository taskRepository) {
        this.personRepository=personRepository;
        this.enterpriseRepository=enterpriseRepository;
        this.bankRepository=bankRepository;
        this.userRepository=userRepository;
        this.demandSubRepository = demandSubRepository;
        this.demandRepository = demandRepository;
        this.fileRepository = fileRepository;
        this.taskRepository=taskRepository;
    }

    public void verifyInformation (VerifyDTO verifyDTO){
        String id=verifyDTO.getId();
        String isPass=verifyDTO.getIsPass();
        BtUser user=userRepository.findByUserId(id);

        if(user.getUserType().equals(CommonConstant.USER_BANK)){
            BtBank btBank=bankRepository.findOne(id);
            btBank.setStatus(isPass);
            bankRepository.save(btBank);
        }else if(user.getUserType().equals(CommonConstant.USER_ENTERPRISE)){
            BtEnterprise btEnterprise=enterpriseRepository.findOne(id);
            btEnterprise.setStatus(isPass);
            enterpriseRepository.save(btEnterprise);
        }else if(user.getUserType().equals(CommonConstant.USER_PERSON)){
            BtPerson btPerson=personRepository.findOne(id);
            btPerson.setStatus(isPass);
            personRepository.save(btPerson);
        }
    }


    public void verifyLogout(VerifyDTO verifyDTO) {
        String id=verifyDTO.getId();
        String isPass=verifyDTO.getIsPass(); //isPass 已注销 未注销
        BtUser user=userRepository.findByUserId(id);

        if(user.getUserType().equals(CommonConstant.USER_BANK)){
            logoutUser(id,isPass);
            BtBank btBank=bankRepository.findOne(id);
            btBank.setStatus(isPass);
            bankRepository.save(btBank);
        }else if(user.getUserType().equals(CommonConstant.USER_ENTERPRISE)){
            logoutUser(id,isPass);
            BtEnterprise btEnterprise=enterpriseRepository.findOne(id);
            btEnterprise.setStatus(isPass);
            enterpriseRepository.save(btEnterprise);
        }else if(user.getUserType().equals(CommonConstant.USER_PERSON)){
            logoutUser(id,isPass);
            BtPerson btPerson=personRepository.findOne(id);
            btPerson.setStatus(isPass);
            personRepository.save(btPerson);
        }
    }

    private void logoutUser(String id, String isPass) {
        if ("已注销".equals(isPass)){
            BtUser user=userRepository.findByUserId(id);
            user.setStatus(CommonConstant.LOGOUT);
            userRepository.save(user);
        }
    }


    public Page<BtDemand> demandsForNewAndMod(Pageable pageable) {
        return demandRepository.findDemands(CommonConstant.DEMAND_SHOWN,pageable);
    }

    public void verifyDemand(VerifyDemandDTO verifyDemandDTO) {
        String operation = verifyDemandDTO.getOperation();

        if(operation.equals(CommonConstant.NEW)){
            verifyNewDemand(verifyDemandDTO);
        }else if(operation.equals(CommonConstant.MODIFY)){
            verifyModifyDemand(verifyDemandDTO);
        }

        }

    private void verifyModifyDemand(VerifyDemandDTO verifyDemandDTO) {
        String id = verifyDemandDTO.getId();
        String isPass = verifyDemandDTO.getIsPass();
        BtDemandSub btDemandSub = demandSubRepository.findOne(id);
        BtDemand btDemand = demandRepository.findOne(btDemandSub.getDemandId());
        Integer times = btDemand.getModifyTimes()+1;
        if(isPass.equals(CommonConstant.PASS)){
            btDemand.setId(btDemandSub.getDemandId());
            btDemand.setCreateTime(btDemand.getCreateTime());
            btDemand.setModifyTime(new Date());
            btDemand.setModifyTimes(times);
            btDemand.setStatus(CommonConstant.UNSIGNED);
            demandRepository.save(btDemand);

            btDemandSub.setState(CommonConstant.PASS);
            btDemandSub.setModifyTime(new Date());
            demandSubRepository.save(btDemandSub);
            //执行解绑原文件和绑新文件操作
            fileRepository.unbindFileNew(id,CommonConstant.EFFECTIVE);
            List<String> list = new ArrayList<String>(Arrays.asList(btDemandSub.getFileIds().split(",")));
            list.forEach(fileId -> {
                fileRepository.update(btDemandSub.getDemandId(), fileId);
            });

        }else{
            //将sub置为无效
            btDemandSub.setState(CommonConstant.REJECT);
            btDemandSub.setModifyTime(new Date());
            demandSubRepository.save(btDemandSub);

        }

    }


    private void verifyNewDemand(VerifyDemandDTO verifyDemandDTO) {
        String id = verifyDemandDTO.getId();
        String isPass = verifyDemandDTO.getIsPass();
        BtDemandSub btDemandSub = demandSubRepository.findOne(id);
        if(isPass.equals(CommonConstant.PASS)){
            BtDemand btDemand = new BtDemand();
            BeanUtils.copyProperties(btDemandSub,btDemand);
            btDemand.setId(btDemandSub.getDemandId());
            btDemand.setCreateTime(new Date());
            btDemand.setModifyTime(new Date());
            btDemand.setStatus(CommonConstant.UNSIGNED);
            demandRepository.save(btDemand);

            btDemandSub.setState(CommonConstant.PASS);
            btDemandSub.setModifyTime(new Date());
            demandSubRepository.save(btDemandSub);
        }else{
            //将sub置为无效
            btDemandSub.setState(CommonConstant.REJECT);
            btDemandSub.setModifyTime(new Date());
            demandSubRepository.save(btDemandSub);
            //将相关文件置为无效
            fileRepository.unbindFileNew(btDemandSub.getDemandId(),CommonConstant.UNEFFECTIVE);
        }
    }


}
